﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.SystemManageForms.InitialTestItemForms
{
    public partial class SetTestItemMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.ToolBll toolBll = new BLL.ToolBll();
        BLL.SysReportTypeBll sysReportTypeBll = new BLL.SysReportTypeBll();
        BLL.TestItemBll testItemBll = new BLL.TestItemBll();
        //List<SysReportType> _SysReportTypeList;
        //List<TestItem> _TestItemList;
        public SetTestItemMainForm()
        {
            InitializeComponent();
        }

        private void SetTestItemMainForm_Load(object sender, EventArgs e)
        {
            //_SysReportTypeList = sysReportTypeBll.LoadEntities(m => m.SysReportTypeID != "4").ToList();
            repositoryItemLookUpEdit1.DataSource = sysReportTypeBll.LoadEntities(m => m.SysReportTypeID != "4").ToList();
            // _TestItemList = toolBll.GetTestItemList();
            checkedListBoxControl1.DisplayMember = "ChineseName";
            checkedListBoxControl1.ValueMember = "TestItemID";
        }

        private void barLargeButtonItem_Save_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barEditItem1.EditValue != null && barEditItem1.EditValue.ToString() != "请选择")
            {
                splashScreenManager1.ShowWaitForm();

                string reportTypeID = Convert.ToString(barEditItem1.EditValue);
                SysReportType type = sysReportTypeBll.LoadEntities(m => m.SysReportTypeID == reportTypeID).First();
                List<TestItem> testItemList = new List<TestItem>();
                for (int i = 0; i < checkedListBoxControl1.ItemCount; i++)
                {
                    CheckState state = checkedListBoxControl1.GetItemCheckState(i);
                    if (state == CheckState.Checked)
                    {
                        string id = Convert.ToString(checkedListBoxControl1.GetItemValue(i));
                        TestItem testItem = testItemBll.LoadEntities(m => m.TestItemID == id).FirstOrDefault();
                        if (testItem != null)
                        {
                            testItemList.Add(testItem);
                        }
                    }
                }

                //sysReportType.TestItems = testItemList;
                sysReportTypeBll.EditSysReportType(reportTypeID, testItemList);
                int res = toolBll.InitStandardWhenInitTestItemChanged(type);
                splashScreenManager1.CloseWaitForm();
                string mesg = String.Format( ResourceOverall.MessBoxSHowCount, res.ToString());
                XtraMessageBox.Show(mesg);

            }
        }

        private void barEditItem1_EditValueChanged(object sender, EventArgs e)
        {
            //repositoryItemLookUpEdit1
            if (barEditItem1.EditValue != null && barEditItem1.EditValue.ToString() != "请选择")
            {
                this.checkedListBoxControl1.DataSource = testItemBll.LoadEntities(m => true).ToList();
                string reportTypeID = Convert.ToString(barEditItem1.EditValue);
                SysReportType type = sysReportTypeBll.LoadEntities(m => m.SysReportTypeID == reportTypeID).First();
                checkedListBoxControl1.BeginUpdate();
                for (int i = 0; i < checkedListBoxControl1.ItemCount; i++)
                {
                    string name = checkedListBoxControl1.GetItemText(i);
                    string id = Convert.ToString(checkedListBoxControl1.GetItemValue(i));
                    TestItem testItem = testItemBll.LoadEntities(m => m.TestItemID == id).First();
                    if (type.TestItems.Where(m => m.TestItemID == testItem.TestItemID).Count() == 1)
                    {
                        checkedListBoxControl1.SetItemCheckState(i, CheckState.Checked);
                    }
                    else
                    {
                        checkedListBoxControl1.SetItemCheckState(i, CheckState.Unchecked);
                    }
                }
                checkedListBoxControl1.EndUpdate();
            }
            else
            {
                checkedListBoxControl1.DataSource = null;
                return;
            }

        }


    }
}